/**
 * 
 */
package com.williamoneil.charts.server.controller.msn;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.williamoneil.charts.shared.FinancialData;

/**
 * @author gudipati
 *
 */
public class MSNEarningsController {

	private final String TH_OPEN = "<th>";
	private final String TH_CLOSE = "</th>";
	
	private final String TD_OPEN = "<td>";
	private final String TD_CLOSE = "</td>";
	
	private final String NA = "NA";
	
	private final String MSN_FINANCIAL_RESULTS_URL = "http://investing.money.msn.com/investments/financial-results/?symbol=%s";
	public List<FinancialData> getFinancialData(final String symbol) throws Exception {
		
		final URL url = new URL(String.format(MSN_FINANCIAL_RESULTS_URL, symbol));
		
		//final String htmlContent = url.getContent().toString();
		
		final BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
		final StringBuffer buffy = new StringBuffer();
		String readLine = reader.readLine(); // first line is header.. we can ignore it
		if(readLine == null) {
			return null;
		} else {
			while(readLine != null) {
				buffy.append(readLine);
				readLine = reader.readLine();
			}
		}
		
		final String htmlContent = buffy.toString();
		
		int index = htmlContent.indexOf("<span>REVENUE - QUARTERLY RESULTS (IN MILLIONS)</span>");
		if(index < 0) {
			return null;
		}
		
		index = htmlContent.indexOf(TH_OPEN, index); // ignore first <th>
		if(index < 0){
			return null;
		}
		
		final DateTimeFormatter dfUtc = DateTimeFormat.forPattern("MM/yy").withZoneUTC();
		
		index = htmlContent.indexOf(TH_OPEN, index+TH_OPEN.length());
		int index2 = htmlContent.indexOf(TH_CLOSE, index);
		String year1 = htmlContent.substring(index+TH_OPEN.length(), index2).trim(); 
		year1 = year1.replace("FY", "");
		year1 = year1.replace("(", "");
		year1 = year1.replace(")", "");
		DateTime year1Dt = dfUtc.parseDateTime(year1.trim());
		
		index = htmlContent.indexOf(TH_OPEN, index2);
		index2 = htmlContent.indexOf(TH_CLOSE, index);
		String year2 = htmlContent.substring(index+TH_OPEN.length(), index2).trim();
		year2 = year2.replace("FY ", "");
		year2 = year2.replace("(", "");
		year2 = year2.replace(")", "");
		DateTime year2Dt = dfUtc.parseDateTime(year2.trim());
		
		index = htmlContent.indexOf(TH_OPEN, index2);
		index2 = htmlContent.indexOf(TH_CLOSE, index);
		String year3 = htmlContent.substring(index+TH_OPEN.length(), index2).trim();
		year3 = year3.replace("FY ", "");
		year3 = year3.replace("(", "");
		year3 = year3.replace(")", "");
		DateTime year3Dt = dfUtc.parseDateTime(year3.trim());
		
		//rev-1st qtr
		index = htmlContent.indexOf("1st Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr1_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr1_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr1_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		//rev-2nd qtr
		index = htmlContent.indexOf("2nd Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr2_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr2_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr2_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		//rev-3rd qtr
		index = htmlContent.indexOf("3rd Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr3_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr3_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr3_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim().replace(",","");
		
		//rev-4th qtr
		index = htmlContent.indexOf("4th Qtr", index2);
		if (index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr4_1 = htmlContent.substring(index + TD_OPEN.length(), index2).trim().replace(",","");

		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr4_2 = htmlContent.substring(index + TD_OPEN.length(), index2).trim().replace(",","");

		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		String qtr4_3 = htmlContent.substring(index + TD_OPEN.length(), index2).trim().replace(",","");
		
		final FinancialData q1_4 = new FinancialData(); 
		q1_4.setDate(year1Dt.toDate());
		if(qtr4_1 != null && !qtr4_1.contains(NA)) {
			q1_4.setRevenues(new BigDecimal(qtr4_1));
		}
		final FinancialData q1_3 = new FinancialData(); 
		q1_3.setDate(year1Dt.minusMonths(3).toDate());
		if(qtr3_1 != null && !qtr3_1.contains(NA)) {
			q1_3.setRevenues(new BigDecimal(qtr3_1));
		}
		final FinancialData q1_2 = new FinancialData(); 
		q1_2.setDate(year1Dt.minusMonths(6).toDate());
		if(qtr2_1 != null && !qtr2_1.contains(NA)) {
			q1_2.setRevenues(new BigDecimal(qtr2_1));
		}
		final FinancialData q1_1 = new FinancialData(); 
		q1_1.setDate(year1Dt.minusMonths(9).toDate());
		if(qtr1_1 != null && !qtr1_1.contains(NA)) {
			q1_1.setRevenues(new BigDecimal(qtr1_1));
		}
		
		final FinancialData q2_4 = new FinancialData(); 
		q2_4.setDate(year2Dt.toDate());
		if(qtr4_2 != null && !qtr4_2.contains(NA)) {
			q2_4.setRevenues(new BigDecimal(qtr4_2));
		}
		final FinancialData q2_3 = new FinancialData(); 
		q2_3.setDate(year2Dt.minusMonths(3).toDate());
		if(qtr3_2 != null && !qtr3_2.contains(NA)) {
			q2_3.setRevenues(new BigDecimal(qtr3_2));
		}
		final FinancialData q2_2 = new FinancialData(); 
		q2_2.setDate(year2Dt.minusMonths(6).toDate());
		if(qtr2_2 != null && !qtr2_2.contains(NA)) {
			q2_2.setRevenues(new BigDecimal(qtr2_2));
		}
		final FinancialData q2_1 = new FinancialData(); 
		q2_1.setDate(year2Dt.minusMonths(9).toDate());
		if(qtr1_2 != null && !qtr1_2.contains(NA)) {
			q2_1.setRevenues(new BigDecimal(qtr1_2));
		}
		
		final FinancialData q3_4 = new FinancialData(); 
		q3_4.setDate(year3Dt.toDate());
		if(qtr4_3 != null && !qtr4_3.contains(NA)) {
			q3_4.setRevenues(new BigDecimal(qtr4_3));
		}
		final FinancialData q3_3 = new FinancialData(); 
		q3_3.setDate(year3Dt.minusMonths(3).toDate());
		if(qtr3_3 != null && !qtr3_3.contains(NA)) {
			q3_3.setRevenues(new BigDecimal(qtr3_3));
		}
		final FinancialData q3_2 = new FinancialData(); 
		q3_2.setDate(year3Dt.minusMonths(6).toDate());
		if(qtr2_3 != null && !qtr2_3.contains(NA)) {
			q3_2.setRevenues(new BigDecimal(qtr2_3));
		}
		final FinancialData q3_1 = new FinancialData(); 
		q3_1.setDate(year3Dt.minusMonths(9).toDate());
		if(qtr1_3 != null && !qtr1_3.contains(NA)) {
			q3_1.setRevenues(new BigDecimal(qtr1_3));
		}
		
		
		//EPS now
		index = htmlContent.indexOf("<span>EARNINGS PER SHARE - QUARTERLY RESULTS</span>");
		if(index < 0) {
			return null;
		}
		
		index = htmlContent.indexOf(TH_OPEN, index); // ignore first <th>
		if(index < 0){
			return null;
		}
		
		index = htmlContent.indexOf(TH_OPEN, index+TH_OPEN.length());
		index2 = htmlContent.indexOf(TH_CLOSE, index);
		year1 = htmlContent.substring(index+TH_OPEN.length(), index2).trim(); 
		year1 = year1.replace("FY ", "");
		year1 = year1.replace("(", "");
		year1 = year1.replace(")", "");
		year1Dt = dfUtc.parseDateTime(year1.trim());
		
		index = htmlContent.indexOf(TH_OPEN, index2);
		index2 = htmlContent.indexOf(TH_CLOSE, index);
		year2 = htmlContent.substring(index+TH_OPEN.length(), index2).trim();
		year2 = year2.replace("FY ", "");
		year2 = year2.replace("(", "");
		year2 = year2.replace(")", "");
		year2Dt = dfUtc.parseDateTime(year2.trim());
		
		index = htmlContent.indexOf(TH_OPEN, index2);
		index2 = htmlContent.indexOf(TH_CLOSE, index);
		year3 = htmlContent.substring(index+TH_OPEN.length(), index2).trim();
		year3 = year3.replace("FY ", "");
		year3 = year3.replace("(", "");
		year3 = year3.replace(")", "");
		year3Dt = dfUtc.parseDateTime(year3.trim());
		
		//rev-1st qtr
		index = htmlContent.indexOf("1st Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr1_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr1_1 != null && !qtr1_1.equalsIgnoreCase(NA)) {
			q1_1.setEpsQtr(new BigDecimal(qtr1_1.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr1_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr1_2 != null && !qtr1_2.equalsIgnoreCase(NA)) {
			q2_1.setEpsQtr(new BigDecimal(qtr1_2.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr1_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr1_3 != null && !qtr1_3.equalsIgnoreCase(NA)) {
			q3_1.setEpsQtr(new BigDecimal(qtr1_3.trim().replace("$", "")));
		}
		
		//rev-2nd qtr
		index = htmlContent.indexOf("2nd Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr2_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr2_1 != null && !qtr2_1.equalsIgnoreCase(NA)) {
			q1_2.setEpsQtr(new BigDecimal(qtr2_1.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr2_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr2_2 != null && !qtr2_2.equalsIgnoreCase(NA)) {
			q2_2.setEpsQtr(new BigDecimal(qtr2_2.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr2_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr2_3 != null && !qtr2_3.equalsIgnoreCase(NA)) {
			q3_2.setEpsQtr(new BigDecimal(qtr2_3.trim().replace("$", "")));
		}
		
		//rev-3rd qtr
		index = htmlContent.indexOf("3rd Qtr", index2);
		if(index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr3_1 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr3_1 != null && !qtr3_1.equalsIgnoreCase(NA)) {
			q1_3.setEpsQtr(new BigDecimal(qtr3_1.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr3_2 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr3_2 != null && !qtr3_2.equalsIgnoreCase(NA)) {
			q2_3.setEpsQtr(new BigDecimal(qtr3_2.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr3_3 = htmlContent.substring(index+TD_OPEN.length(), index2).trim();
		if(qtr3_3 != null && !qtr3_3.equalsIgnoreCase(NA)) {
			q3_3.setEpsQtr(new BigDecimal(qtr3_3.trim().replace("$", "")));
		}
		
		//rev-4th qtr
		index = htmlContent.indexOf("4th Qtr", index2);
		if (index < 0) {
			return null;
		}
		index = htmlContent.indexOf(TD_OPEN, index);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr4_1 = htmlContent.substring(index + TD_OPEN.length(), index2).trim();
		if(qtr4_1 != null && !qtr4_1.equalsIgnoreCase(NA)) {
			q1_4.setEpsQtr(new BigDecimal(qtr4_1.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr4_2 = htmlContent.substring(index + TD_OPEN.length(), index2).trim();
		if(qtr4_2 != null && !qtr4_2.equalsIgnoreCase(NA)) {
			q2_4.setEpsQtr(new BigDecimal(qtr4_2.trim().replace("$", "")));
		}
		
		index = htmlContent.indexOf(TD_OPEN, index2);
		index2 = htmlContent.indexOf(TD_CLOSE, index);
		qtr4_3 = htmlContent.substring(index + TD_OPEN.length(), index2).trim();
		if(qtr4_3 != null && !qtr4_3.equalsIgnoreCase(NA)) {
			q3_4.setEpsQtr(new BigDecimal(qtr4_3.trim().replace("$", "")));
		}
		
		q1_4.setEpsT4Q(getEPST4Q(q1_4, q1_3, q1_2, q1_1));
		q1_3.setEpsT4Q(getEPST4Q(q1_3, q1_2, q1_1, q2_4));
		q1_2.setEpsT4Q(getEPST4Q(q1_2, q1_1, q2_4, q2_3));
		q1_1.setEpsT4Q(getEPST4Q(q1_1, q2_4, q2_3, q2_2));
		
		q2_4.setEpsT4Q(getEPST4Q(q2_4, q2_3, q2_2, q2_1));
		q2_3.setEpsT4Q(getEPST4Q(q2_3, q2_2, q2_1, q3_4));
		q2_2.setEpsT4Q(getEPST4Q(q2_2, q2_1, q3_4, q3_3));
		q2_1.setEpsT4Q(getEPST4Q(q2_1, q3_4, q3_3, q3_2));
		
		final List<FinancialData> fins = new ArrayList<FinancialData>();
		fins.add(q1_4);
		fins.add(q1_3);
		fins.add(q1_2);
		fins.add(q1_1);
		fins.add(q2_4);
		fins.add(q2_3);
		fins.add(q2_2);
		fins.add(q2_1);
		fins.add(q3_4);
		fins.add(q3_3);
		fins.add(q3_2);
		fins.add(q3_1);
		
		return fins;
	}
	
	private static BigDecimal getEPST4Q(final FinancialData q1, final FinancialData q2, final FinancialData q3, final FinancialData q4) {
		if(q1 == null || q2 == null || q3 == null || q4 == null) {
			return null;
		}
		
		BigDecimal epsT4Q = q1.getEpsQtr();
		if(epsT4Q == null) {
			epsT4Q = q2.getEpsQtr();
		} else {
			if(q2.getEpsQtr() != null) {
				epsT4Q.add(q2.getEpsQtr());
			}
		}
		
		if(epsT4Q == null) {
			epsT4Q = q3.getEpsQtr();
		} else {
			if(q3.getEpsQtr() != null) {
				epsT4Q.add(q3.getEpsQtr());
			}
		}
		
		if(epsT4Q == null) {
			epsT4Q = q4.getEpsQtr();
		} else {
			if(q4.getEpsQtr() != null) {
				epsT4Q.add(q4.getEpsQtr());
			}
		}
		
		return epsT4Q;
	}
}
